import axios from 'axios';

const temp={
  axios:axios,
  get:function(info){
    let params={};
    if(info.data)
      params=info.data;
    axios
      .get(info.url,{params:params})
      .then(function(response){
          if(info.callback){
            info.callback({status: 1, data: response.data});
          }
      })
      .catch(function(err){
        if(info.callback){
          info.callback({status:-1,data:err});
        }
      })
  },
  post:function(info){
    axios
      .post(info.url,null,{
        data:info.data,
        params:info.params
      })
      .then(function(response){
        if(info.callback){
          info.callback({status: 1, data: response.data});
        }
      })
      .catch(function(err){
        if(info.callback){
          info.callback({status:-1,data:err});
        }
      })
  },
  config:function(info){
    axios({
      method:info.type,
      url:info.url,
      data:info.data,//是作为请求主体被发送的数据 只适用于这些请求方法 "PUT", "POST", 和 "PATCH"
      params:info.params,//是即将与请求一起发送的 URL 参数
      responseType:info.responseType,
      timeout:1000*1,
      headers:info.headers
    })
      .then(function(response){
        if(info.callback){
          info.callback({status: 1, data: response.data});
        }
      })
      .catch(function(err){
        if(info.callback){
          info.callback({status:-1,data:err});
        }
      })
  },
  post_form:function(info){
    const options = {
      method: 'POST',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      data: info.data,
      url:info.url
    };
    axios(options)
      .then(function(response){
        if(info.callback){
          info.callback({status: 1, data: response.data});
        }
      })
      .catch(function(err){
        if(info.callback){
          info.callback({status:-1,data:err});
        }
      })
  }
};
export default temp;
